package com.weavernorth.attendance;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: ecology_erdos
 * @description: 年假补发定时类
 * @author: zm
 * @create: 2019-12-23 18:13
 **/


public class AnnualLeaveTimer extends BaseCronJob {
    public static Log log = LogFactory.getLog(AnnualLeaveTimer.class.getName());

    @Override
    public void execute() {
        //基础类
        BaseBean bb = new BaseBean();
        //数据库操作
        RecordSet rs = new RecordSet();
        //补发年假工具类
        AnnualLeaveUtil annualLeaveUtil = new AnnualLeaveUtil();
        //获取当前日期
        Date date = new Date();
        //格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = sdf.format(date);
        //获取多长时间补发年假
        int intWorkYear = Util.getIntValue(bb.getPropValue("annualMess", "annLeaveTime"));
        log.info("得到入职多长时间补发年假:" + intWorkYear);
        //sql获取当前满足补发年假的人;sql注释:cus_fielddata表为自定义数据表,field31为入职时间的值,获取满足补发年假的入职人员
        String sqlYear = "select hr.id,hr.companystartdate rzrq,hr.workstartdate sccjgz,hr.workyear," +
                "to_char(to_date(hr.workstartdate,'yyyy-mm-dd') + interval '365' day(4), 'yyyy-mm-dd') oneyear, " +
                //工龄满10年的日期 ,field28 为首次参加工作日期
                "to_char(to_date(hr.workstartdate,'yyyy-mm-dd') + interval '3650' day(4), 'yyyy-mm-dd') tenyear, " +
                //工龄满20年的日期,field28 为首次参加工作日期
                "to_char(to_date(hr.workstartdate,'yyyy-mm-dd') + interval '7300' day(4), 'yyyy-mm-dd') twentyyear" +
                " from hrmresource hr  " +
                " where   hr.companystartdate is not null  " +
                "and hr.companystartdate =" +
                "(select to_char(sysdate - interval '" + intWorkYear + "' day(4), 'yyyy-mm-dd') from dual)";
                //  " and cs.scopeid=1 ";
        // and cs.field33=?";
        log.info("获取可以补发年假的入职人员:" + sqlYear + ",当前日期:" + formatDate);
        rs.executeQuery(sqlYear);
        while (rs.next()) {
            //人员id
            String strUserid = Util.null2String(rs.getString("id"));
            //入职月份
            String strMonth = Util.null2String(rs.getString("rzrq"));
            //参加工作满1年的日期
            String strOneYear=Util.null2String(rs.getString("oneyear"));
            //满10年的日期
            String strTenYear = Util.null2String(rs.getString("tenyear"));
            //满20年的日期
            String strTwenYear = Util.null2String(rs.getString("twentyyear"));
            //首次参加工作日期
            String strJoinDate = Util.null2String(rs.getString("sccjgz"));
            //工龄
           // Double douWorkYear = Util.getDoubleValue(rs.getString("workyear"));


            //根据人员计算补发的年假
             annualLeaveUtil.getAnnualLeaveTime(strUserid,strMonth, strOneYear,strTenYear, strTwenYear, strJoinDate,formatDate);


        }


    }
}
